package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.AccountCorpDTO;
import com.financing.wap.dto.AccountDetailDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IAccountService;
import com.financing.wap.service.IRebateService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.RandomUtils;
import com.financing.wap.vo.AccountCorpVo;
import com.financing.wap.vo.AccountDetailVo;
import com.financing.wap.vo.AmountVO;
import com.financing.wap.web.controller.BaseController;

@Controller("apiAccountController")
@RequestMapping("/api/account")
public class AccountController extends BaseController {
	
	Logger logger = Logger.getLogger(getClass());
	@Autowired
	private IRebateService rebateService;
	
	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private IUserService userService;

//	/**
//	 * 我的帐户首页
//	 * @return
//	 */
//	@RequestMapping("/user")
//	@ResponseBody
//	public ResultMap account(HttpServletRequest request, HttpServletResponse response){
//		
//		UserDTO userDTO = getSession(true).getUser();
//		ResultMap result = new ResultMap();
//		if(userDTO == null){
//			result.setStatus(STATUS.ERROR);
//			result.setMessage("用户未登录 ！");
//			return result;
//		}
//
//		MyAccountVo vo = rebateService.calcMyAccounts(userDTO.getId());
//		Map<String, Object> paramMap = BeanUtil.transBean2Map(vo);
//		
//		result.setAttributes(paramMap);
//		result.setStatus(STATUS.SUCCESS);
//		result.setMessage("success！");
//		return result;
//	}
	
	
	/**
	 * 机构账户首页
	 * @return
	 */
	@RequestMapping("/index")
	@ResponseBody
	public ResultMap accountInfo(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		String type = loginUser.getUType();
		if(Constants.FA_TYPE_CORP.equals(type)){
			logger.info("进入机构账号");
			result = corpAccount(request,response);
		}else{
			logger.info("进入个人账号");
			result = userAccount(request,response);
		}

		return result;
	}
	
	/**
	 * 机构账户组成
	 * @return
	 */
	@RequestMapping("/detail")
	@ResponseBody
	public ResultMap accountDetail(HttpServletRequest request, HttpServletResponse response,AccountCorpVo vo){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String type = loginUser.getUType();
		if(Constants.FA_TYPE_CORP.equals(type)){
			logger.info("进入机构账号");
			result = corpAccountDetail(request,response,vo);
		}else{
			logger.info("进入个人账号");
			result = userAccountDetail(request,response,  accountService.getFaAccountByUserId(loginUser.getId()).getId());
		}
		return result;
	}
	
	
	/**
	 * 机构绩效
	 * @return
	 */
	@RequestMapping("/corpAmount")
	@ResponseBody
	public ResultMap corpAmount(HttpServletRequest request, HttpServletResponse response,AmountVO vo){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String corpId = loginUser.getId();
		String type = loginUser.getUType();
		if(!Constants.FA_TYPE_CORP.equals(type)){
			result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
			result.setMessage("违规操作，非机构账号！");
			return result;
		}
		
		try{
			vo.setCorpId(corpId);
			String pageNo = request.getParameter("pageNo");
			if(StringUtils.isBlank(pageNo)){
				pageNo="1";
			}
			Pager pager = new Pager();
			Integer count = accountService.getCorpUserAmountCount(vo);
			pager.setTotalCount(count);
			pager.setPageSize(vo.getPageSize());
			pager.setPageNo(Integer.parseInt(pageNo));
			vo.setStart(pager.getStartIndex());
			List<Map<String,Object>>  userAmountList = accountService.getCorpUserAmount(vo);
			List<Map<String,Object>>  deptAmountList = accountService.getCorpDeptAmount(corpId);
			Map<String,Object>  allAmount = accountService.getCorpAllAmount(corpId);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("success！");
			result.setAttribute("userAmountList", userAmountList);
			result.setAttribute("deptAmountList", deptAmountList);
			result.setAttribute("allAmount", allAmount);
		}catch(Exception e){
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	
	/**
	 * FA个人账户首页
	 * @return
	 */

	private ResultMap userAccount(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String userId = loginUser.getId();
		String type = loginUser.getUType();
		if(Constants.FA_TYPE_CORP.equals(type)){
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
			result.setMessage("违规操作，非FA账号！");
			return result;
		}
		try{
			AccountCorpDTO accountUserDTO = accountService.getFaAccountByUserId(userId);
			BigDecimal todayAllRebates = accountService.todayAllRebates();
			if(StringUtils.isBlank(request.getParameter("b"))){
				todayAllRebates = RandomUtils.getTodayAllRebatesBaseMun(5).add(todayAllRebates);
			}
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("success！");
			result.setAttribute("dto", accountUserDTO);
			result.setAttribute("todayAllRebates", todayAllRebates);
			
		}catch(Exception e){
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/**
	 * 个人账户明细
	 * @return
	 */

	private ResultMap userAccountDetail(HttpServletRequest request, HttpServletResponse response, Integer accountId){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String userId = loginUser.getId();
		String type = loginUser.getUType();
		if(Constants.FA_TYPE_CORP.equals(type)){
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
			result.setMessage("违规操作，非FA账号！");
			return result;
		}
		
		try{
			AccountDetailVo vo = new AccountDetailVo();
			vo.setAccountId(accountId);
			vo.setUserId(userId);
			String pageNo = request.getParameter("pageNo");
			if(StringUtils.isBlank(pageNo)){
				pageNo="1";
			}
			Pager pager = new Pager();
			Integer count = accountService.getUserAccountDetailCount(vo);
			if(count==0){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				result.setMessage("not fount Object!");
				return result;
			}
			pager.setTotalCount(count);
			pager.setPageSize(vo.getPageSize());
			pager.setPageNo(Integer.parseInt(pageNo));
			vo.setStart(pager.getStartIndex());
			List<AccountDetailDTO> list = accountService.getUserAccountDetailList(vo);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("success！");
			result.setAttribute("list", list);
		}catch(Exception e){
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/**
	 * 机构账户首页
	 * @return
	 */

	private ResultMap corpAccount(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String corpId = loginUser.getId();
		String type = loginUser.getUType();
		if(!Constants.FA_TYPE_CORP.equals(type)){
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
			result.setMessage("违规操作，非机构账号！");
			return result;
		}
		try{
			AccountCorpVo vo = new AccountCorpVo();
			vo.setuManagerId(corpId);
			String uDeptIdStr = request.getParameter("uDeptId");
			if(StringUtils.isNotBlank(uDeptIdStr)){
				vo.setuDeptId(Integer.valueOf(uDeptIdStr));
			}
			AccountCorpDTO dto = accountService.getAccountCorpInfo(vo);
			
			BigDecimal todayAllRebates = accountService.todayAllRebates();
			if(StringUtils.isBlank(request.getParameter("b"))){
				//平台虚拟返佣开关，当没有b参数时添加平台虚拟返佣
				todayAllRebates = RandomUtils.getTodayAllRebatesBaseMun(5).add(todayAllRebates);
			}
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("success！");
			result.setAttribute("dto", dto);
			result.setAttribute("todayAllRebates", todayAllRebates);
			
		}catch(Exception e){
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/**
	 * 机构账户组成
	 * @return
	 */

		private ResultMap corpAccountDetail(HttpServletRequest request, HttpServletResponse response,AccountCorpVo vo){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String corpId = loginUser.getId();
		String type = loginUser.getUType();
		if(!Constants.FA_TYPE_CORP.equals(type)){
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
			result.setMessage("违规操作，非机构账号！");
			return result;
		}
		
		try{
			vo.setuManagerId(corpId);
			String uDeptIdStr = request.getParameter("uDeptId");
			if(StringUtils.isNotBlank(uDeptIdStr)){
				vo.setuDeptId(Integer.valueOf(uDeptIdStr));
			}
			String pageNo = request.getParameter("pageNo");
			if(StringUtils.isBlank(pageNo)){
				pageNo="1";
			}
			Pager pager = new Pager();
			Integer count = accountService.getAccountDetailCorpCount(vo);
			pager.setTotalCount(count);
			pager.setPageSize(vo.getPageSize());
			pager.setPageNo(Integer.parseInt(pageNo));
			vo.setStart(pager.getStartIndex());
			List<AccountCorpDTO> list = accountService.getAccountDetailCorpList(vo);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setMessage("success！");
			result.setAttribute("list", list);
		}catch(Exception e){
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
}
